import request from '../../utils/request'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    musicPic:'',
    art:'',
    musicName:'',
    musicUrl:'',
    zhuanji:'',
    isPlay:false,
    songsId:'',
    startTime:'0:00',
    endTime:'0:00',
    cw:0,
    isCenter:true,
    lrcArr:[],
    top:'500rpx'
  },

  changCenter(){
    this.setData({
      isCenter:!this.data.isCenter
    })
  },
isPlaying(){
  if(!this.data.musicUrl) return;
  let playBool = !this.data.isPlay
  this.setData({
    isPlay:playBool
  })
  let music = wx.getBackgroundAudioManager()
  playBool?music.play():music.pause()
},

async musicPlay(){
  let musicUrlData = await request('/song/url',{id:this.data.songsId})
  this.setData({
    musicUrl:musicUrlData.data[0].url
  })
  let bgAudioManager = wx.getBackgroundAudioManager()
  if(!this.data.musicUrl) return;
  bgAudioManager.src = this.data.musicUrl
  bgAudioManager.title = this.data.art + '-' + this.data.musicName
  bgAudioManager.onTimeUpdate(()=>{
    let s = ~~(bgAudioManager.currentTime%60);
    s = s<10 ? "0"+s:s;
    let timeing = Math.trunc(bgAudioManager.currentTime)
    let index = this.data.lrcArr.findIndex(v=>v.time == timeing)
    if(index!==-1){
      this.setData({
        top:-index*40+"rpx"
      })
    }
    this.setData({
      startTime:~~(bgAudioManager.currentTime/60)+":"+s,
      cw:bgAudioManager.currentTime*60 / bgAudioManager.duration
    })
  })
  bgAudioManager.onEnded(()=>{bgAudioManager.src = this.data.musicUrl})
  bgAudioManager.onPause(()=>{this.setData({isPlay:false})})
  bgAudioManager.onPlay(()=>{this.setData({isPlay:true})})
},
lrcHander(s){
  let lrcArr=[];
  s.replace(/\[(.*?)\](.*)/g,(v,$1,$2)=>{
    // console.log($1,$2);
    $1=$1.replace(/(.*)\:(.*)\.(.*)/,(v,p1,p2)=>{
      return Number(p1)*60+Number(p2)
    })
    let o = {time:$1,text:$2}
    lrcArr.push(o)
  });
  this.setData({lrcArr})
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let musicId = options.ids
    let musicDetailData = await request('/song/detail',{ids:musicId})
    let lyric = await request('/lyric',{id:musicId})
    this.lrcHander(lyric.lrc.lyric)
    let dt = musicDetailData.songs[0].dt;
    this.setData({
      songsId: musicId,
      musicPic:musicDetailData.songs[0].al.picUrl,
      art: musicDetailData.songs[0].ar[0].name,
      musicName:musicDetailData.songs[0].name,
      zhuanji:musicDetailData.songs[0].al.name,
      endTime: `${Math.floor(dt/1000/60)}:${~~(dt/1000%60)<10?'0'+~~(dt/1000%60):~~(dt/1000%60)}`
    })
    this.musicPlay()
    this.isPlaying()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})